// Copyright 2022 jeff.li. and/or its affiliates.


#include <tbir/runtime/container.h>
#include <tbir/runtime/container_private.h>
#include <tbir/runtime/module.h>
#include <tbir/runtime/registry.h>

namespace tbir::runtime {

    TBIR_REGISTER_GLOBAL("runtime.ModuleGetSource").set_body_typed([](Module mod, Unicode fmt) {
        return String(mod->GetSource(fmt.encode())).decode();
    });

    TBIR_REGISTER_GLOBAL("runtime.ModuleImportsSize").set_body_typed([](Module mod) {
        return static_cast<int64_t>(mod->imports().size());
    });

    TBIR_REGISTER_GLOBAL("runtime.ModuleGetImport").set_body_typed([](Module mod, int index) {
        return mod->imports().at(index);
    });

    TBIR_REGISTER_GLOBAL("runtime.ModuleGetTypeKey").set_body_typed([](Module mod) {
        return String(mod->type_key()).decode();
    });

    TBIR_REGISTER_GLOBAL("runtime.ModuleLoadFromFile")
    .set_body_typed([](Unicode name, Unicode fmt) {
        return Module::LoadFromFile(name.encode(), fmt.encode());
    });

    TBIR_REGISTER_GLOBAL("runtime.ModuleSaveToFile")
    .set_body_typed([](Module mod, Unicode name, Unicode fmt) {
        mod->SaveToFile(name.encode(), fmt.encode());
    });

    TBIR_REGISTER_GLOBAL("runtime.GetCodegenModulePtrName").set_body_typed([]() {
        return String(symbol::library_module_ctx).decode();
    });

}  // namespace tbir::runtime
